/** @file    Fact.cpp
 *  @author  Alessandro Polo
 *  @version $Id: Fact.cpp 2833 2010-08-02 23:12:57Z alex $
 *  @brief
 * File containing methods for the wosh::Fact class.
 * The header for this class can be found in Fact.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include <core/Fact.h>
 
 #include <core/ObjectAllocator.h>


using namespace std;
namespace wosh {

 WOSH_REGISTER(wosh::Fact, "wosh::MessageContentGeneric", 1.01, _static_Fact)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS

Fact::Fact() 
	: MessageContentGeneric() { }

Fact::Fact( const std::string& method )
	: MessageContentGeneric( method ) { }

Fact::Fact( const std::string& method, ISerializable* data )
	: MessageContentGeneric(method, data) { }

Fact::Fact( const Fact& m )
	: MessageContentGeneric(m) { }

//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////// OPERATORS

Fact& Fact::operator=(const Fact& m){
	if (this == &m) return *this; // same object?
	(MessageContentGeneric&)*this = (const MessageContentGeneric&)m;
	return *this;
}

bool Fact::operator==(const Fact& other) const {
	return ( (const MessageContentGeneric&)*this == (const MessageContentGeneric&)other );
 }

/////////////////////////////////////////////////////////////////////////////////////////////////// OPERATORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_REGISTER(wosh::Fact_ListChanged, "wosh::Fact", 1.0, _static_Fact_ListChanged )

Fact_ListChanged::Fact_ListChanged( ISerializable* item, FACT_LIST_EVENT list_event, const char* method_name )
	: Fact( method_name ) {
	List* list = new List(2);
	list->push_back(item);
	list->push_back<Variant>( (int)list_event );
	setData(list);
 }
Fact_ListChanged::Fact_ListChanged( const Variant& item_key, FACT_LIST_EVENT list_event, const char* method_name )
	: Fact( method_name ) {
	List* args = List::createFrom<Variant>( item_key, (int)list_event );
	setData(args);
 }

const ISerializable* Fact_ListChanged::getItem() const {
	if ( !hasData() ) return NULL;
	if ( getData()->isKindOf<List>() ) {
		const List* list = getData()->as<List>();
		if ( list == NULL ) return NULL;
		if ( list->isEmpty() ) return NULL;
		return list->itemAt(0);
	 }
	return getData();
 }

const Variant* Fact_ListChanged::getItemKey() const {
	if ( !hasData() ) return NULL;
	if ( content->isKindOf<Variant>() )
		return content->as<Variant>();
	if ( getData()->isKindOf<List>() ) {
		const List* list = getData()->as<List>();
		if ( list == NULL ) return NULL;
		if ( list->size() < 2 ) return NULL;
		if ( list->itemAt(0) == NULL ) return NULL;
		return list->itemAt(0)->as<Variant>();
	 }
	return NULL;
 }

int Fact_ListChanged::getListAction() const {
	if ( !hasData() ) return (int)EVENT_LISTITEM_UNKNOWN;
	if ( getData()->isKindOf<List>() ) {
		const List* list = getData()->as<List>();
		if ( list == NULL ) return (int)EVENT_LISTITEM_UNKNOWN;
		if ( list->size() < 2 ) return (int)EVENT_LISTITEM_UNKNOWN;
		if ( list->itemAt(1) == NULL ) return (int)EVENT_LISTITEM_UNKNOWN;
		const Variant* val = list->itemAt(1)->as<Variant>();
		if ( val == NULL ) return (int)EVENT_LISTITEM_UNKNOWN;
		if ( !val->isNumeric() ) return (int)EVENT_LISTITEM_UNKNOWN;
		return val->toInteger((int)EVENT_LISTITEM_UNKNOWN);
	 }
	return (int)EVENT_LISTITEM_UNKNOWN;
 }

std::string Fact_ListChanged::toString() const {
	const Variant* var = getItemKey();
	if ( var != NULL )
		return Utilities::format("%s[%s,%s]", getClassNameOnlyOf(getClassName()), var->toString().c_str(), Fact_ListChanged::getListEventAsString((FACT_LIST_EVENT)getListAction()) );
	else
		return Utilities::format("%s[%s]", getClassNameOnlyOf(getClassName()), Fact_ListChanged::getListEventAsString((FACT_LIST_EVENT)getListAction()) );
 }

const char* Fact_ListChanged::getListEventAsString( FACT_LIST_EVENT list_event ) {
	switch(list_event) {
		case EVENT_LISTITEM_ADDED:		return "ADDED";
		case EVENT_LISTITEM_UPDATED:	return "UPDATED";
		case EVENT_LISTITEM_REMOVED:	return "REMOVED";
		case EVENT_LISTITEM_UNKNOWN:
		default:						return "UNKNOWN";
	 }
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

}; // namespace wosh
